package com.snake.vargoappstore.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.snake.vargoappstore.R;
import com.snake.vargoappstore.db.AppInfo;

import java.util.Random;

/**
 * Created by chenshuai on 16-12-18.
 */

public class ListViewItem extends FrameLayout {

    private TextView mTitle;
    private TextView mDesc;
    private RatingBar mStar;
    private Button mButton;
    private int mPosition;
    private ImageView mIcon;

    public ListViewItem(Context context) {
        super(context);
        init(context);
    }

    public ListViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ListViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.listview_item_main, this);
        mIcon = (ImageView) findViewById(R.id.iv_listView_item_icon);
        mTitle = (TextView) findViewById(R.id.tv_listView_item_title);
        mDesc = (TextView) findViewById(R.id.tv_listView_item_desc);
        mStar = (RatingBar) findViewById(R.id.ratingB_listView_item_star);
        mButton = (Button) findViewById(R.id.btn_listView_item_install);
    }

    public void bindData(int position ,AppInfo data){
        mPosition = position;
        mTitle.setText(data.getName());
        mDesc.setText(data.getSize()+"");
        mStar.setRating(new Random().nextInt(5));
        Glide.with(getContext()).load(data.getIconUrl()).
                placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mIcon);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}
